// components/football/football.js
var model = require('../../model/model.js')
var netWork = require('../../utils/netWork.js');
import {pickInfo} from "../../utils/publicMethods";
//选择城市
var show = false;
var addressInfo = {};
var item = {};
var isClick = true;
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        bg:{
            type: String,
            value: ''
        },
        type:{
            type: String,
            value: ''
        },
        topHeight:{
            type: String,
            value: ''
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        imagesUrl:getApp().globalData.imagesUrl,//图片路径
        tipsFlag:false,//是否显示竞猜提交领取成功提示
        errTipsFlag:false,//是否显示错误提示
        subDialog:false,//是否显示提交弹窗
        materialInfo:{},//邮寄信息
        address: '请选择地区',//地区文字显示
        addressMsg:"",//详细地址
        footballData:{},//世界杯竞猜信息
        item: {//城市选择器信息
            show: show
        },
        formValue: {},//form表单邮寄信息
        disabledFlag:false,//禁止输入
        footballPrizeInfo:{},//竞猜卷奖励信息
        isLucky:-1,//中没中奖 0 未中奖 1已中奖
        gameStartsFlag:false,//比赛开始
        msg:""//错误提示
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //关闭提示弹窗并改变奖励信息状态
        closeTips(e){
            //1未绑定场次 8提交竞猜比分 10过期
            let status = e.currentTarget.dataset.status;
            let prizeOrderId = this.data.footballPrizeInfo.prizeOrderId;
            this.triggerEvent('setPrizeStatus',{prizeOrderId,status});
        },
        //点击选择城市按钮显示picker-view
        translate: function (e) {
            if(this.data.footballData.status == 8){
                return;
            }
            model.animationEvents(this, 0, true, 400);
        },
        //城市隐藏picker-view
        hiddenFloatView: function (e) {
            model.animationEvents(this, 200, false, 400);
            if (e.target.dataset.id == '555') {
                return;
            }
            if (JSON.stringify(addressInfo) == '{}' || (this.data.item.value[0] == 0 && this.data.item.value[1] == 0 && this.data.item.value[2] == 0)) {
                addressInfo = {
                    "receiveProvinceId": '110000',
                    "receiveCityId": '110100',
                    "receivceDistrictId": '110101',
                    "receiveProvinceName": '北京市',
                    "receiveCityName": '北京市',
                    "receiveDistrictName": '东城区'
                };

            }
            this.setData({
                address: addressInfo.receiveProvinceName + ' ' + addressInfo.receiveCityName + ' ' + addressInfo.receiveDistrictName,
            })
        },
        //城市滑动事件
        bindChange: function (e) {
            model.updateAreaData(this, 1, e);
            item = this.data.item;
            addressInfo = {
                "receiveProvinceId": item.provinces[item.value[0]?item.value[0]:0].id,
                "receiveCityId": item.citys[item.value[1]?item.value[1]:0].id,
                "receivceDistrictId": item.countys[item.value[2]?item.value[2]:0].id,
                "receiveProvinceName": item.provinces[item.value[0]?item.value[0]:0].name,
                "receiveCityName": item.citys[item.value[1]?item.value[1]:0].name,
                "receiveDistrictName": item.countys[item.value[2]?item.value[2]:0].name
            };
        },
        //表单提交按钮,校验
        formSubmit: function (e) {
            if(this.data.footballData.status == 8){
                return;
            }
            //防止重复点击或者点击过快
            console.log("点击了");
            if (!isClick) {
                console.log("重复返回~");
                return;
            }
            if (isClick) {
                isClick = false;
                //定时器
                setTimeout(function () {
                    isClick = true;
                }, 3000); //3秒内不能重复点击
            }
            let detailAddress = e.detail.value.detailAddress;
            let homeGoal = e.detail.value.homeGoal;
            let vistingGoal = e.detail.value.vistingGoal;
            if (homeGoal.trim() == "" || vistingGoal.trim() == "") {
                wx.showToast({
                    title: '请输入得分',
                    icon: 'none'
                })
                return;
            }
            if (this.data.address == '请选择地区') {
                wx.showToast({
                    title: '请选择地区',
                    icon: 'none'
                })
                return;
            }
            if (detailAddress.trim() == "") {
                wx.showToast({
                    title: '请输入详细地址',
                    icon: 'none'
                })
                return;
            }
            if(detailAddress.length > 60){
                wx.showToast({
                    title: '详细地址不能超过60个文字',
                    icon: 'none'
                })
                return;
            }
            this.setData({
                formValue: e.detail.value,
                addressMsg:detailAddress
            })
            let materialInfo = {};
            materialInfo['receiveProvinceId'] = addressInfo.receiveProvinceId;
            materialInfo['receiveCityId'] = addressInfo.receiveCityId;
            materialInfo['receivceDistrictId'] = addressInfo.receivceDistrictId;
            materialInfo['receiveProvinceName'] = addressInfo.receiveProvinceName;
            materialInfo['receiveCityName'] = addressInfo.receiveCityName;
            materialInfo['receiveDistrictName'] = addressInfo.receiveDistrictName;
            materialInfo['receiveAddress'] = detailAddress;
            wx.setStorageSync('footballMaterialInfo', materialInfo)
            this.revicePrize();
        },
        /**
         * 点击领取,进行实物奖兑奖
         */
        revicePrize: function () {
            this.isLoading(true, "正在提交...");
            const that = this;
            //兑奖
            netWork.POST('worldcup/pickSubmit', {
                data: {
                    userId: wx.getStorageSync('userId'),
                    prizeOrderId:that.data.footballPrizeInfo.prizeOrderId,
                    worldCupId:that.data.footballData.worldcupInfo.worldCupId,
                    homeGoal:that.data.formValue.homeGoal,
                    visitingGoal:that.data.formValue.vistingGoal,
                    receiveProvinceId: addressInfo.receiveProvinceId, //省
                    receiveCityId: addressInfo.receiveCityId, //市
                    receivceDistrictId: addressInfo.receivceDistrictId, //区
                    receiveProvinceName:addressInfo.receiveProvinceName,
                    receiveCityName: addressInfo.receiveCityName,
                    receiveDistrictName: addressInfo.receiveDistrictName,
                    receiveAddress: that.data.formValue.detailAddress, //地址
                },
                success: function (res) {
                    that.isLoading(false);
                    if (res.data.retCode == undefined) {
                        wx.redirectTo({
                            url: '../prompt/prompt?retCode=' + apiCode.FAIL.code + '&retMsg=' + apiCode.FAIL.msg,
                        })
                    }
                    if (res.data.retCode == "0000") {
                        that.setData({
                            tipsFlag:true,
                            subDialog:false
                        })
                    } else if (res.data.retCode == "10052") {
                        setTimeout(function () {
                            wx.showToast({
                                title: '收货信息不支持表情等特殊字符，请修改后再提交哦~',
                                icon: 'none',
                                duration: 3000
                            })
                        }, 100)
                    } else if (res.data.retCode == "10093") {
                        that.setData({
                            errTipsFlag:true,
                            subDialog:false
                        })
                    }else if (res.data.retCode == "10094") {
                        that.setData({
                            gameStartsFlag:true,
                            subDialog:false,
                            msg:res.data.retMsg
                        })
                    }else {
                        wx.redirectTo({
                            url: '../prompt/prompt?retCode=' + res.data.retCode + '&retMsg=' + res.data.retMsg,
                        })
                    }
                },
                fail: function (res) {
                    that.isLoading(false);
                    wx.redirectTo({
                        url: '../prompt/prompt?retCode=' + apiCode.DRAWFAIL.code + '&retMsg=' + apiCode.DRAWFAIL.msg,
                    })
                }
            });
        },
        //loading提示
        isLoading(isLoading, loadingText) {
            this.setData({
              isLoading: isLoading,
              loadingText: loadingText
            })
        },
        //当前场次已开赛,刷新获取最近的场次
        async closeGameStartsTips(e){
            this.getData(e);
            this.setData({
                getData:true,
                gameStartsFlag:false
            })
        },
        async getData(e){
            let res = await pickInfo(this.data.footballPrizeInfo.prizeOrderId);
            this.setData({
                footballData:res.data.data
            })
            let footballData = res.data.data;
            //判断是否还有场次
            if(JSON.stringify(footballData.worldcupInfo) != "{}"){//有 显示竞猜弹窗
                this.setData({
                    subDialog:true
                })
            }else{//没有 显示错误提示弹窗
                this.setData({
                    errTipsFlag:true
                })
                return;
            }
            let m = {};
            //判断从哪里反显收货信息
            if(footballData.materialInfo != null && footballData.materialInfo.receiveProvinceName != null){//从接口反显
                m = footballData.materialInfo;
            }else{//从缓存反显
                m = wx.getStorageSync('footballMaterialInfo');
            }
            //反显收货信息
            if (m) {
                this.setData({
                    materialInfo: m,
                    address: m.receiveProvinceName + ' ' + m.receiveCityName + ' ' + m.receiveDistrictName,
                    addressMsg: m.receiveAddress
                })
                addressInfo = this.data.materialInfo;
            } else {
                let userPhone = wx.getStorageSync('userPhone');
                let m1 = {};
                if (userPhone != "" && userPhone != null && userPhone != undefined) {
                    m1['receivePhone'] = userPhone;
                }
                this.setData({
                 materialInfo: m1
                })
            }
            //判断当前奖励竞猜状态
            if(footballData.status == 8){
                if(footballData.worldcupInfo.status == 0){
                    this.setData({
                        disabledFlag:true,
                        isLucky:0
                    })
                }else{
                    let isLucky = 0;
                    if(footballData.worldcupInfo.isLucky == null){
                        isLucky = 1;
                    }else{
                        isLucky = footballData.worldcupInfo.isLucky == 0 ? 1:2;
                    }
                    this.setData({
                        disabledFlag:true,
                        isLucky
                    })
                }
            }
        }
    }
})
